package com.ruoyi.web.controller.member;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.coupons.domain.Coupons;
import com.ruoyi.coupons.domain.CouponsMember;
import com.ruoyi.coupons.service.ICouponsMemberService;
import com.ruoyi.coupons.service.ICouponsService;
import com.ruoyi.shopping.domain.MallCoupons;
import com.ruoyi.shopping.domain.MallCouponsMember;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.member.service.IMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 *
 * @author ruoyi
 * @date 2021-10-22
 */
@RestController
@RequestMapping("/member/member")
public class MemberController extends BaseController
{
    @Autowired
    private IMemberService memberService;
    @Autowired
    private ICouponsMemberService couponsMemberService;
    @Autowired
    private ICouponsService couponsService;

    /**
     * 优惠券页面根据手机号查询客户信息
     */
    @GetMapping("/tempPhone")
    public AjaxResult tempPhone( String phone)
    {
        Member member = new Member();
        member.setPhone(phone);
        List<Member> list = memberService.selectMemberList(member);
        return AjaxResult.success(list);
    }


    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(Member member)
    {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('member:member:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Member member)
    {
        List<Member> list = memberService.selectMemberList(member);
        ExcelUtil<Member> util = new ExcelUtil<Member>(Member.class);
        return util.exportExcel(list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(memberService.selectMemberById(id));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Member member)
    {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Member member)
    {
        return toAjax(memberService.updateMember(member));
    }
    /**
     * 发放优惠券
     */
    @PreAuthorize("@ss.hasPermi('member:member:edit')")
    @Log(title = "发放优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/canUseCoupons")
    public AjaxResult canUseCoupons(Long memberId,Long couponsId)
    {
        Coupons coupons = couponsService.selectCouponsById(couponsId);
        CouponsMember couponsMember = new CouponsMember();
        couponsMember.setCouponId(coupons.getId());
        couponsMember.setUseState(DicConstants.SYS_N.getCode());
        couponsMember.setBelongType(coupons.getBelongType());
        couponsMember.setMemberId(memberId);
        couponsMember.setMoney(coupons.getMoney());
        couponsMember.setNewUserState(coupons.getNewUserState());
        couponsMember.setMinPrice(coupons.getMinPrice());
        couponsMember.setPrimaryKey(coupons.getPrimaryKey());
        couponsMember.setCanUseState(DicConstants.SYS_Y.getCode());

        if(DicConstants.SYS_Y.getCode().equals(coupons.getFixedTimeState())){
            couponsMember.setUseStartTime(new Date());
            couponsMember.setUseEndTime(DateUtils.addDate(coupons.getValidDay().intValue()));
        }else{
            couponsMember.setUseStartTime(coupons.getUseStartTime());
            couponsMember.setUseEndTime(coupons.getUseEndTime());
        }
        couponsMember.setReceiveType(DicConstants.RECEIVE_TYPE_Gift.getCode());
        couponsMember.setCouponType(coupons.getCouponType());
        couponsMember.setCouponName(coupons.getCouponName());
        couponsMember.setStoreId(coupons.getStoreId());
        couponsMember.setGiveUserId(getUserId());
        couponsMember.setCreateBy(getUserId());
        couponsMember.setCreateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(couponsMemberService.insertCouponsMember(couponsMember));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('member:member:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(memberService.deleteMemberByIds(ids));
    }
}
